package com.chinawlx.wlxteacher.utils;

import android.util.Log;

/**
 * 作者:吴名
 * 时间:16/8/2
 * 描述:史上最好用的Log工具
 * 1，普通log TAG自动产生，格式为：类名.方法名(行号)；建议普通log使用  log.i/d/...
 * 2，超级log TAG默认为LogUtils 格式为：类名.方法名.行号，并且双击可跳转，但耗能是普通log的20倍，故默认关闭打印，建议发布前关闭  log.printI/D....
 */
public class WLXLogUtil {
    private static final String TAG = "yyc";

    public static boolean allow = false;
    /*******
     * 是否打印super log
     *********/
    public static boolean isSuperLog = false;

    private static String getPrefix() {
        String prefix = "%s.%s(L:%d)";

        StackTraceElement caller = Thread.currentThread().getStackTrace()[4];
        String callerClazzName = caller.getClassName();
        callerClazzName = callerClazzName.substring(callerClazzName.lastIndexOf(".") + 1);
        prefix = String.format(prefix, callerClazzName, caller.getMethodName(), caller
                .getLineNumber());
        return prefix;
    }

    /**
     * Send a DEBUG log message,蓝色
     */
    public static void d(String content) {
        if (allow) {
            Log.d(getPrefix(), content);
        }
    }

    /**
     * Send an ERROR log message,红色
     */
    public static void e(String content) {
        if (allow) {
            Log.e(getPrefix(), content + "");
        }
    }

    /**
     * Send an INFO log message,绿色
     */
    public static void i(String content) {
        if (allow) {
            Log.i(getPrefix(), content);
        }

    }

    /**
     * Send a VERBOSE log message,黑色
     */
    public static void v(String content) {
        if (allow) {
            Log.v(getPrefix(), content);
        }
    }

    /**
     * Send a WARN log message,黄色
     */
    public static void w(String content) {
        if (allow) {
            Log.w(getPrefix(), content);
        }
    }

    public static void e(String content, Throwable tr) {
        if (allow) {
            Log.e(getPrefix(), content, tr);
        }
    }

    public static void e(Throwable tr) {
        if (allow) {
            Log.e(getPrefix(), tr.getMessage());
        }
    }


    /*******************super log 带行号并且点击可跳转的log**************************/
    /**
     * Send an INFO log message,绿色
     */
    public static void printI(String log) {
        if (allow && isSuperLog) {
            Log.i(TAG, log + ">" + callMethodAndLine());
        }
    }

    /**
     * Send a DEBUG log message,蓝色
     */
    public static void printD(String log) {
        if (allow && isSuperLog) {
            Log.d(TAG, log + ">" + callMethodAndLine());
        }
    }

    /**
     * Send an ERROR log message,红色
     */
    public static void printE(String log) {
        if (allow && isSuperLog) {
            Log.e(TAG, log + ">" + callMethodAndLine());
        }
    }

    /**
     * Send a VERBOSE log message,黑色/白色
     */
    public static void printV(String log) {
        if (allow && isSuperLog) {
            Log.v("TAG", log + ">" + callMethodAndLine());
        }
    }

    /**
     * Send a WARN log message,黄色
     */
    public static void printW(String log) {
        if (allow && isSuperLog) {
            Log.w(TAG, log + ">" + callMethodAndLine());
        }
    }


    private static String callMethodAndLine() {
        StringBuilder result = new StringBuilder("at ");
        StackTraceElement thisMethodStack = (new Exception()).getStackTrace()[2];
        result.append(thisMethodStack.getClassName() + ".").append(thisMethodStack.getMethodName
                ()).append("(" + thisMethodStack.getFileName()).append(":" + thisMethodStack
                .getLineNumber() + ")  ");
        return result.toString();
    }
}
